package com.tj.jinzao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

/**
 * Created by LiuYX on 2016/7/26.
 */

public class Base_Adapter extends BaseAdapter {
    public Context context;
    public List list;
    //是否需要第一行显示header
    private boolean isFirst;
    public <T extends View> T findView(View v, int id) {
        return (T) v.findViewById(id);
    }
    OnItemClickLinstener onItemClickLinstener;
    public interface OnItemClickLinstener{
        void onItemClick(int position);
    }
    public void setOnItemClickLinstener(OnItemClickLinstener onItemClickLinstener){
        this.onItemClickLinstener=onItemClickLinstener;
    }

    public Base_Adapter(Context context, List list, boolean isFirst){
        this.context=context;
        this.list=list;
        this.isFirst=isFirst;

    }
    public void setData(List list){
        this.list=list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(isFirst==true){
            if(list.size()>0){
                return list.size()+1;
            }
            return 1;
        }else {
            if(list.size()>0){
                return list.size();
            }
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        if(isFirst==true){
            if(list.size()>0) {
                return list.get(position-1);
            }

        }else {
            if(list.size()>0) {
                return list.get(position);
            }
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return null;
    }
    public void displayImageCitcular(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(com.yalantis.ucrop.R.color.ucrop_color_grey).bitmapTransform(new CircleCrop())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context.getApplicationContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(options)
                .into(imageView);

    }
    public void displayImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(com.yalantis.ucrop.R.color.ucrop_color_grey)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        Glide.with(context.getApplicationContext())
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(options)
                .into(imageView);

    }
    public void goActivity(Context context, Class<?> cla){
        Intent to=new Intent();
        to.setClass(context,cla);
        context.startActivity(to);
    }
    public void goActivityValues(Context context, Class<?> cla){
        Intent to=new Intent();
        to.setClass(context,cla);
        context.startActivity(to);
    }

}
